package com.central.apps.controller;

import com.central.app.service.HandlerService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/deptWork")
public class DeptWork {

    @Autowired
    private HandlerService handlerService;

    @RequestMapping("/dept")
    public Result deptWork(){
        List<Map<String, Object>> lists =new ArrayList<>();
        Map<String, Object> map =new HashMap<>();
        //获取当前登陆人
        LoginAppUser user = SysUserUtil.getLoginAppUser();
        //获取部门编码
        String deptcode =user.getDeptcode();
        //String deptcode ="D0072";
        //查询部门总人数
        String Sql1 ="select count(id) as users from sys_user where  deptcode ='"+deptcode+"';";
        List<Map<String, Object>> bySql1 =handlerService.findBySql(Sql1);
        //放到返回结果 map中
        map.put("name", "部门总人数");
        map.put("count", MapUtils.getString(bySql1.get(0),"users"));
        map.put("yip", "");
        lists.add(map);

        //查询当年工单完成率
        //获取当年工单总数
        Map<String, Object> map2 =new HashMap<>();
        //String Sql2 ="select count(a.id) as counts from gq_workorder a left JOIN gq_workordertask b on a.WONUM=b.WONUM where a.deptcode ='"+deptcode+"' and YEAR(b.SCHEDFINISH) =YEAR(NOW());";
        String Sql2 ="select count(id) as counts from gq_workorder where deptcode ='"+deptcode+"' and YEAR(createtime) =YEAR(NOW());";
        List<Map<String, Object>> bySql2 = handlerService.findBySql(Sql2);
        //获取已完成工单总数
        //String Sql3 ="select count(a.id) as counts from gq_workorder a left JOIN gq_workordertask b on a.WONUM=b.WONUM where a.deptcode ='"+deptcode+"' and YEAR(b.SCHEDFINISH) =YEAR(NOW()) and YEAR(a.updatetime) =YEAR(NOW()) and a.status='已审批';";
        String Sql3 ="select count(id) as counts from gq_workorder where deptcode ='"+deptcode+"' and YEAR(createtime) =YEAR(NOW()) and status='已审批';";
        List<Map<String, Object>> bySql3 = handlerService.findBySql(Sql3);
        //当年工单已完成数量
        double ywc =MapUtils.getDoubleValue(bySql3.get(0),"counts");
        int ywc2 =MapUtils.getIntValue(bySql3.get(0),"counts");
        //当年总数
        double all =MapUtils.getDoubleValue(bySql2.get(0),"counts");
        int all2 =MapUtils.getIntValue(bySql2.get(0),"counts");
        String yearwcl ="0%";
        if(ywc2!=0){
            //完成率
            double wcl =ywc/all;
            DecimalFormat df   = new DecimalFormat("######0.00");
            yearwcl =df.format(wcl);
            if(wcl ==1){
                yearwcl ="100%";
            }else if("0".equals(yearwcl.substring(2,3))){
                yearwcl =yearwcl.substring(3,4)+"%";
            }else{
                yearwcl =yearwcl.substring(2,4)+"%";
            }
        }
        //放到返回结果 map中
        map2.put("name", "工作完成率");
        map2.put("count", yearwcl);
        map2.put("yip", "总量："+ywc2 +"/"+ all2+"项");
        lists.add(map2);
        Map<String, Object> map4 =new HashMap<>();
        map4.put("name", "已完成的工作");
        map4.put("count", ywc2);
        map4.put("yip", "总量："+all2+"项");
        lists.add(map4);

        //获取当年各种状态工单及数量
        List<String> statusLists =new ArrayList<>();
        statusLists.add("审批中");statusLists.add("暂停");statusLists.add("草稿");
        for(int j=0; j<statusLists.size(); j++){
            Map<String, Object> map3 =new HashMap<>();
            String status =statusLists.get(j);
            //String sql5 ="select count(a.id) as counts from gq_workorder a left JOIN gq_workordertask b on a.WONUM=b.WONUM where a.deptcode ='"+deptcode+"' and YEAR(b.SCHEDFINISH) =YEAR(NOW()) and a.status ='"+status+"';";
            String sql5 ="select count(id) as counts from gq_workorder where deptcode ='"+deptcode+"' and YEAR(createtime) =YEAR(NOW()) and status='"+status+"';";
            List<Map<String, Object>> bySql = handlerService.findBySql(sql5);
            String name="";
            if("审批中".equals(status)){
                name="进行中的工作";
            }else if("暂停".equals(status)){
                name="已暂停的工作";
            }else{
                name="未开始的工作";
            }
            double wocounts =MapUtils.getDoubleValue(bySql.get(0),"counts");
            int wocounts2 =MapUtils.getIntValue(bySql.get(0),"counts");
            String wowcl ="0%";
            if(wocounts2!=0){
                //完成率
                double yearwowcl =wocounts/all;
                DecimalFormat df   = new DecimalFormat("######0.00");
                wowcl =df.format(yearwowcl);
                if(yearwowcl ==1){
                    wowcl ="100%";
                }else if("0".equals(wowcl.substring(2,3))){
                    wowcl =wowcl.substring(3,4)+"%";
                }else{
                    wowcl =wowcl.substring(2,4)+"%";
                }
            }
            map3.put("name", name);
            map3.put("count", wocounts2);
            map3.put("yip", "占比："+wowcl);
            lists.add(map3);
        }
        return Result.succeed(lists,"succeed");
    }

    //获取7天的日期
    @RequestMapping("/seven")
    public Result seven(){
        //获取当前登陆人
        LoginAppUser user = SysUserUtil.getLoginAppUser();
        //获取部门编码
        String deptcode =user.getDeptcode();
        //String deptcode ="D0072";
        List<Map<String, Object>> lists =new ArrayList<>();
        Map<String, Object> map =new HashMap<>();
        String sql6 ="select DATE_SUB(curdate(),INTERVAL +6 DAY) day1 union "+
                "select DATE_SUB(curdate(),INTERVAL +5 DAY) day2 union "+
                "select DATE_SUB(curdate(),INTERVAL +4 DAY) day3 union "+
                "select DATE_SUB(curdate(),INTERVAL +3 DAY) day4 union "+
                "select DATE_SUB(curdate(),INTERVAL +2 DAY) day5 union "+
                "select DATE_SUB(curdate(),INTERVAL +1 DAY) day6 union "+
                "select DATE_SUB(curdate(),INTERVAL +0 DAY) day7 ;";
        List<Map<String, Object>> bySql6 = handlerService.findBySql(sql6);
        List<String> xlists =new ArrayList<>();
        List<String> ylists =new ArrayList<>();
        for(int i=0; i<bySql6.size(); i++){
            String date =MapUtils.getString(bySql6.get(i),"day1");
            xlists.add(date);
            String sql7 ="select count(id)as counts from gq_workorder where deptcode ='"+deptcode+"' and DATE_FORMAT(createtime,'%Y-%m-%d')='"+date+"' and status ='已审批';";
            List<Map<String, Object>> bySql7 = handlerService.findBySql(sql7);
            String counts =MapUtils.getString(bySql7.get(0),"counts");
            ylists.add(counts);
        }
        map.put("x",xlists);
        map.put("y",ylists);
        lists.add(map);
        return Result.succeed(lists,"操作成功");
    }


    @RequestMapping("/dateRange")
    public Result dateRange(@RequestParam String begindate, @RequestParam String enddate){
        //获取日期范围内各种状态工单的数量
        Map<String, Object> map =new HashMap<>();
        //获取当前登陆人
        LoginAppUser user = SysUserUtil.getLoginAppUser();
        //获取部门编码
        String deptcode =user.getDeptcode();
        //String deptcode ="D0072";
        String sql4 ="select status,count(id)as counts from gq_workorder where deptcode ='"+deptcode+"' and createtime BETWEEN '"+begindate+"' and '"+enddate+"' group by status;";
        List<Map<String, Object>> bySql4 = handlerService.findBySql(sql4);
        Map<String, Object> map2 =new HashMap<>();
        for(int i=0;i< bySql4.size();i++){
            String status =MapUtils.getString(bySql4.get(i),"status");
            String counts =MapUtils.getString(bySql4.get(i),"counts");
            map2.put(status ,counts);
        }
        map.put("日期范围内各种状态工单及数",map2);
        return Result.succeed(map,"succeed");
    }

    //获取资产台账设备分类及数量
    @RequestMapping("/assetClassCounts")
    public Result assetClassCounts(){
        List<Map<String, Object>> lists =new ArrayList<>();
        String sql ="select classifydescribe,count(id) as counts from bl_asset group by classifydescribe;";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        for(int i=0; i< bySql.size(); i++){
            Map<String, Object> map =new HashMap<>();
            String classifydescribe =MapUtils.getString(bySql.get(i),"classifydescribe");
            String counts =MapUtils.getString(bySql.get(i),"counts");
            map.put("name",classifydescribe);
            map.put("value",counts);
            lists.add(map);
        }
        return Result.succeed(lists,"succeed");
    }

    //物资采购数据显示
    @RequestMapping("/itemBuy")
    public Result itemBuy(){
        List<Map<String, Object>> lists =new ArrayList<>();
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        //获取当前登陆人
        LoginAppUser user = SysUserUtil.getLoginAppUser();
        //获取部门编码
        String deptcode =user.getDeptcode();
        //String deptcode = "D0069";
        //获取部门.年月a.deptcode ,
//        String deptdatesql ="select DATE_FORMAT(a.createtime,'%Y-%m') as date from bl_cgdan a LEFT JOIN bl_cgdanitem b on a.cgpaynum =b.cgpaynum where a.status ='已审批' and a.deptcode='"+deptcode+"' GROUP BY DATE_FORMAT(a.createtime,'%Y-%m');";
//        List<Map<String, Object>> bySql = handlerService.findBySql(deptdatesql);
        //按照部门.年月分组查找已审批采购单编号
        String symbol = "\"'\"";
        //
        String cgdansql ="select GROUP_CONCAT(CONCAT("+symbol+",cgpaynum,"+symbol+")) as cgpaynum,DATE_FORMAT(createtime,'%Y-%m') as cgdate from bl_cgdan where deptcode ='"+deptcode+"' and status ='已审批' and DATE_FORMAT(createtime,'%Y')='"+year+"' group by DATE_FORMAT(createtime,'%Y-%m')";
        List<Map<String, Object>> cgpaynumList = handlerService.findBySql(cgdansql);
        DecimalFormat df = new DecimalFormat("#.00");
        Map<String,Object> cgMap = new HashMap<>();
        if(cgpaynumList!=null && cgpaynumList.size()>0){
            for(Map<String, Object> mp : cgpaynumList){
                String cgpaynum = MapUtils.getString(mp,"cgpaynum");
                String cgdate = MapUtils.getString(mp,"cgdate");
                cgMap.put(cgdate,cgpaynum);
            }
        }

        ArrayList<String> thenumList = new ArrayList<>();
        ArrayList<String> costList = new ArrayList<>();
        Map<String,Object> returnMap = new HashMap<>();
        for(int i=1;i<13;i++){
            //String deptcode = MapUtils.getString(bySql.get(i),"deptcode");
            String nowMonth = year+"-"+i;
            if(i<10){
                nowMonth = year+"-0"+i;
            }

            if(cgMap.containsKey(nowMonth)){
                String cgpaynum =MapUtils.getString(cgMap,nowMonth);
                //获取当前采购单下子表字段
                String soninfossql ="select sum(thenum) as thenums,sum(cost) as costs from bl_cgdanitem where cgpaynum  in ("+cgpaynum+")";
                List<Map<String, Object>> bySql2 = handlerService.findBySql(soninfossql);
                if(bySql2!=null && bySql2.size()>0){
                    String thenum = df.format(MapUtils.getDouble(bySql2.get(0),"thenums"));
                    String cost = df.format(MapUtils.getDouble(bySql2.get(0),"costs"));
                    thenumList.add(thenum);
                    costList.add(cost);
                }else{
                    thenumList.add("0");
                    costList.add("0");
                }
                //System.out.println(df.format(thenum));
            }else{
                thenumList.add("0");
                costList.add("0");
            }
        }
        returnMap.put("thenumList",thenumList);
        returnMap.put("costList",thenumList);
        return  Result.succeed(returnMap,"succeed");
    }

}
